package org.xteam.sled.semantic.exp;

public class ExpCondition extends Exp {

	public static final int EQ = 0;
	public static final int NE = 1;
	public static final int GT = 2;
	public static final int GE = 3;
	public static final int LE = 4;
	public static final int LT = 5;
	
	private int cond;
	private Exp left;
	private Exp right;
	
	public ExpCondition(int cond, Exp left, Exp right) {
		this.cond = cond;
		this.left = left;
		this.right = right;
		left.setParent(this);
		right.setParent(this);
	}

	public int condition() {
		return cond;
	}

	public Exp right() {
		return right;
	}

	public Exp left() {
		return left;
	}
	
	@Override
	public boolean equals(Object o) {
		return o instanceof ExpCondition
			&& ((ExpCondition)o).cond == cond
			&& ((ExpCondition)o).left.equals(left)
			&& ((ExpCondition)o).right.equals(right);
	}
	
	public TermList toTermList() {
		return new ExpSub(left, right).toTermList();
	}

	@Override
	public void visit(IExpVisitor visitor) {
		visitor.visitCondition(this);
	}
	
	public String toString() {
		return left + opName(cond) + right;
	}

	private static String opName(int cond) {
		if (cond == EQ)
			return " = ";
		if (cond == NE)
			return " != ";
		if (cond == GE)
			return " >= ";
		if (cond == GT)
			return " > ";
		if (cond == LE)
			return " <= ";
		if (cond == LT)
			return " < ";
		throw new RuntimeException("should not be reached");
	}

	public static int reverseOp(int condition) {
		switch (condition) {
		case EQ:
			return EQ;
		case NE:
			return NE;
		case GT:
			return LE;
		case GE:
			return LT;
		case LT:
			return GE;
		case LE:
			return GT;
		}
		throw new RuntimeException();
	}
	
}
